<?php

/*
	[SupeSite] (C) 2007-2009 Comsenz Inc.
	$Id: $
*/

if(!defined('IN_SUPESITE')) {
	exit('Access Denied');
}
session_start();
//2015.5.27  yangliang
@include_once(S_ROOT.'./function/register.func.php');
@include_once(S_ROOT.'./class/sms.class.php');
@include_once(S_ROOT.'./class/api.class.php');
@include_once(S_ROOT.'./class/Easemob.class.php');
$op = $_GET['op'] ? trim($_GET['op']) : '';

if($_SGLOBAL['supe_uid']) {
	showmessage('请退出当前登录', S_URL);
}

if(postget('refer')) {
	$refer = postget('refer');
} else {
	if(!empty($_SERVER['HTTP_REFERER'])) {
		$refer = $_SERVER['HTTP_REFERER'];
	} else {
		$refer = S_URL_ALL;
	}
}

if(empty($op)) {

	if(empty($_SCONFIG['allowregister'])) {
		showmessage('not_open_registration');
	}

	if(submitcheck('registersubmit')) {

		//已经注册用户
		if($_SGLOBAL['supe_uid']) {
			showmessage('registered', 'space.php');
		}

		if(empty($_SCONFIG['noseccode'])) {
			if(!ckseccode($_POST['seccode'])) {
				showmessage('incorrect_code');
			}
		}
        if(empty($_POST['phonenum'])){
            showmessage('电话号码不能为空！');
        }
        $phonenum = strtoupper(sha1($_POST['phonenum']));
        $currtime = strtotime(date('Y-m-d H:i:s',time()));
        //手机验证码与session中的验证码是否一致
        if($_POST['phonenum']!=$_SESSION['mobile'] or $_POST['mobile_code']!=$_SESSION['mobile_code'] or empty($_POST['phonenum']) or empty($_POST['mobile_code'])){
            showmessage('验证码输入错误！');
        }else if(($currtime - $_SESSION['code_time']) > 300){
            $_SESSION['mobile'] = '';
            $_SESSION['mobile_code'] = '';
            showmessage('验证码失效，请重新获取！');
        }else{

            $_SESSION['mobile'] = '';
            $_SESSION['mobile_code'] = '';
        }
        //密码与确认密码是否一致
		if($_POST['password'] != $_POST['password2']) {
			showmessage('password_inconsistency');
		}

		if(!$_POST['password'] || $_POST['password'] != addslashes($_POST['password'])) {
			showmessage('profile_passwd_illegal');
		}
		$username = $_POST['username'];
		$password = $_POST['password'];
		$email = $_POST['email'];

		

//        if(!empty($username)){
//            if(!check_username($username)){
//                showmessage('user_name_is_not_legitimate');
//            }

//            if(check_usernameexists($username)){
//                showmessage('user_name_already_exists');
//            }
//        }
        if(!empty($email)){
            if(!check_emailformat($email)){
                showmessage('email_format_is_wrong');
            }

            if(check_emailexists($email, $_POST['phonenum'])){
                showmessage('email_has_been_registered');
            }
        }


        //若用户为填写用户名，则默认生成
        if(empty($_POST['username'])){
            $username ='FN_'.substr(uniqid(mt_rand()),-6);
        }else{
            $username = $_POST['username'];
        }

			$setarr = array(
				'username' => $username,
				'groupid' => 16,
				'email' => $email,
				'password' => $_POST['password'], //本地密码随机生成
				'phonenum' => $_POST['phonenum'],
				'dateline' => $_SGLOBAL['timestamp'],
				'updatetime' => $_SGLOBAL['timestamp'],
				'lastlogin' => $_SGLOBAL['timestamp'],
				'ip' => $_SGLOBAL['onlineip']
			);
			//更新本地用户库
		//	inserttable('user', $setarr, 0, true);


        $modelClass = 'app\com\simtoon\models\User';
        $rows = YiiComm::executeByMethod($modelClass,'excuteData',$setarr);
        if($rows == trim('success')){
            setCustomer($phonenum); //2015.6.2  yangliang 查询注册者手机号是否为经纪人客户
			//设置cookie
			ssetcookie('auth', authcode("$setarr[password]\t$setarr[uid]", 'ENCODE'), 2592000);
			ssetcookie('loginuser', $username, 31536000);

			showmessage('registered', '/login');
        }else{
            showmessage('对不起，注册失败!');
        }
	}

	$register_rule = $_SCONFIG['registerrule'];
	$title = $lang['site_reg'];
	$refer = rawurldecode($refer);
	include template('site_register');

} elseif($op == "checkphonenum"){
    $send_code = $_GET['send_code'];
    if(empty($_SESSION['send_code']) or $send_code!=$_SESSION['send_code']){
        //防用户恶意请求
        showmessage('请求超时，请刷新页面后重试');
    }
    $phonenum = $_GET['phonenum'];

    if(empty($phonenum)) {
        showmessage('电话号码不能为空！');
    }
    if(!check_phonenum($phonenum)) {
        showmessage('不是有效号码！');

    }


    if(check_phonenumexists($phonenum)) {
        showmessage('phonenum_already_exists');
    } else {

            showmessage('succeed');

    }
} elseif($op == "checkcode") {

    $r_code = $_GET['code'];
    if(!empty($_SESSION['code']) && $r_code == $_SESSION['code'] && !empty($r_code)){

        $code = rand(100000,999999);

        $content = "您的验证码为：".$code."(5分钟内有效，感谢您使用美家邦图，请填写完成验证)。【美家邦图】";
        $content = iconv("UTF-8", "GB2312//IGNORE", $content);
        $mobile = $_GET['phonenum'];
        $res = sendSMS($mobile,$content);
     //   $res = 100;
        if(trim($res) == 100){
            $_SESSION['code_time'] = strtotime(date('Y-m-d H:i:s',time()));
            $_SESSION['mobile'] = $mobile;
            $_SESSION['mobile_code'] = $code;
            showmessage('succeed');

        }else{
            showmessage('对不起，验证失败！请检查手机号码是否正确');
        }
    }else{
        showmessage('验证码错误！');
    }

} elseif($op == "checkseccode") {
	if(empty($_SCONFIG['noseccode'])) {
		if(ckseccode(trim($_GET['seccode']))) {
			showmessage('succeed');
		} else {
			showmessage('incorrect_code');
		}
	}
}

?>